//
//  AppListView.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/7.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import SDWebImage
import UIKit
//违章代码 实时路况 违章代码 今日油价
class AppListView: UIView {
    
    var applArray = Array<AppModel>()
    override init(frame: CGRect){
        
        super.init(frame: frame)
        
        let layout = UICollectionViewFlowLayout.init()
        let itemW = kScreenW / 4
        layout.itemSize = .init(width: itemW, height: 80)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        
        let collectionView = UICollectionView.init(frame: .init(x: 0, y: 5, width: kScreenW, height: kScreenH - kTabBarH - kStatusBarH - 255), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.showsVerticalScrollIndicator = false
        collectionView.tag = 1
        collectionView.register(AppViewCell.self, forCellWithReuseIdentifier: "cell")
        self.addSubview(collectionView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置图片
    func setAppImage(applArray: Array<AppModel>) {
        self.applArray = applArray
        let collect:UICollectionView = self.viewWithTag(1) as! UICollectionView
        collect.reloadData()
    }
}
extension AppListView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.applArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AppViewCell
        if self.applArray.count>0 {
            let index = indexPath.row
            let model: AppModel = self.applArray[index]
            cell.title.text = model.name
            cell.appBtn.setBackgroundImage(UIImage(named: model.url), for: .normal)
            cell.appBtn.tag = index
            let hvc = HomeViewController()
            cell.appBtn.addTarget(hvc, action: #selector(hvc.appClick(sender:)), for: UIControlEvents.touchUpInside)
        }
        return cell
    }
}
